﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace RainEngine.Effects
{
    /// <summary>
    /// 简单地形使用的Effect类，它是对SimpleTerrain.fx的封装。
    /// </summary>
    public class SimpleTerrainEffect:DiffuseLightEffectWrapper
    {
        EffectParameter diffuseTextureParam;
        EffectParameter diffuseTextureUVTileParam;
        EffectParameter detailTextureParam;
        EffectParameter detailTextureUVTileParam;
        EffectParameter detailTextureEnabledTileParam;

        /// <summary>
        /// 地形不需要计算镜面高光，所以重写了基类属性，不进行任何操作
        /// </summary>
        public override Vector3 EyePosition
        {
            set{}
        }
        
        /// <summary>
        /// 获取或设置漫反射纹理。
        /// </summary>
        public Texture2D DiffuseTexture
        {
            get { return diffuseTextureParam.GetValueTexture2D(); }
            set { diffuseTextureParam.SetValue(value); }
        }

        /// <summary>
        /// 获取或设置漫反射纹理的平铺次数。
        /// </summary>
        public Vector2 DiffuseTextureUVTile
        {
            get { return diffuseTextureUVTileParam.GetValueVector2(); }
            set { diffuseTextureUVTileParam.SetValue(value); }
        }
        
        /// <summary>
        /// 获取或设置细节纹理。
        /// </summary>
        public Texture2D DetailTexture
        {
            get { return detailTextureParam.GetValueTexture2D(); }
            set { detailTextureParam.SetValue(value); }
        }

        /// <summary>
        /// 获取或设置纹理的平铺次数。
        /// </summary>
        public Vector2 DetailTextureUVTile
        {
            get { return detailTextureUVTileParam.GetValueVector2(); }
            set { detailTextureUVTileParam.SetValue(value); }
        }

        /// <summary>
        /// 获取或设置是否将细节纹理施加到漫反射纹理上。
        /// </summary>
        public bool DetailTextureEnabled
        {
            get { return detailTextureEnabledTileParam.GetValueBoolean(); }
            set { detailTextureEnabledTileParam.SetValue(value); }
        }

        #region 方法

        /// <summary>
        /// 创建一个新的SimpleTerrainEffect对象。
        /// </summary>
        public SimpleTerrainEffect():base(RainGE .SimpleTerrainEffect){}

        /// <summary>
        /// 缓存effect的参数。
        /// </summary>
        protected override void CacheEffectParameters()
        {
            base.CacheEffectParameters();
            diffuseTextureParam = effect.Parameters["DiffuseTexture"];
            diffuseTextureUVTileParam = effect.Parameters["DiffuseTextureUVTile"];
            detailTextureParam = effect.Parameters["DetailTexture"];
            detailTextureUVTileParam = effect.Parameters["DetailTextureUVTile"];
            detailTextureEnabledTileParam = effect.Parameters["DetailTextureEnabled"];
        }

        public override void OnApply()
        {
            base.OnApply();

            // 重新设置shader index参数
            if ((dirtyFlags & EffectDirtyFlags.ShaderIndex) != 0)
            {
                int shaderIndex = 0;

                if (fogEnabled)
                    shaderIndex += 1;                

                if (lightingEnabled)
                {
                    shaderIndex += 2;
                }

                shaderIndexParam.SetValue(shaderIndex);

                dirtyFlags &= ~EffectDirtyFlags.ShaderIndex;
            }
        }

        #endregion
    }
}